package com.itheima.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mall.pojo.Resource;
import com.itheima.mall.pojo.ResourceCategory;
import com.itheima.ums.mapper.ResourceCategoryMapper;
import com.itheima.ums.service.UmsResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UmsResourceCategoryServiceImpl implements UmsResourceCategoryService {

    @Autowired
    private ResourceCategoryMapper resourceCategoryMapper;

    /**
     * 添加后台资源分类
     *
     * @param resourceCategory
     * @return
     */
    public Integer create(ResourceCategory resourceCategory) {
        resourceCategory.setCreateTime(new Date());
        return resourceCategoryMapper.insert(resourceCategory);
    }

    /**
     * 根据ID删除后台资源
     *
     * @param id
     * @return
     */
    public Integer deleteById(Long id) {
        return resourceCategoryMapper.deleteById(id);
    }

    /**
     * 查询所有后台资源分类
     *
     * @return
     */
    public List<ResourceCategory> listAll() {
        LambdaQueryWrapper<ResourceCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ResourceCategory::getSort);
        return resourceCategoryMapper.selectList(queryWrapper);
    }

    /**
     * 修改后台资源分类
     *
     * @param id
     * @param resourceCategory
     * @return
     */
    public Integer updateById(Long id, ResourceCategory resourceCategory) {
        LambdaQueryWrapper<ResourceCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResourceCategory::getId, id);
        return resourceCategoryMapper.update(resourceCategory, queryWrapper);
    }
}
